package com.lkd.job;

import com.lkd.common.VMSystem;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.xxl.job.core.log.XxlJobLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Component
@Slf4j
public class UserJob {
    @Autowired
    UserService userService;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 每日工单量列表初始化
     * @param
     * @return
     * @throws Exception
     */
    @XxlJob("workCountInitJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) throws Exception{
        XxlJobLogger.log("每日工单初始化");
        //查询用户列表
        //构建数据（zset）
        userService.list().forEach(
                user->{
                    if(!user.getRoleCode().equals("1001")){
                        String key = VMSystem.REGION_TASK_KEY_PREF+ LocalDateTime.now()
                                .format(DateTimeFormatter.ofPattern("yyyyMMdd"))
                                +"."+user.getRegionId()
                                +"." +user.getRoleCode();
                        redisTemplate.opsForZSet().add(key,user.getId(),0);
                        redisTemplate.expire(key, Duration.ofDays(1));
                    }
                }
        );
        return ReturnT.SUCCESS;
    }
}
